package leetcodecompetition.D367;

import org.junit.jupiter.api.Test;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: Siantar
 * @Date: 2023-10-15-10:52
 * @Description: 1.0
 */
public class T100084 {
    public String shortestBeautifulSubstring(String s, int k) {
        int n = s.length();
        String res = "";
        Deque<Integer> deque = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == '1') {
                deque.offer(i);
                if (deque.size() >= k) {
                    while (deque.size() > k) deque.poll();
                    String p = s.substring(deque.getFirst(), deque.getLast() + 1);
                    if (res == "" || p.length() < res.length() || p.length() == res.length() && res.compareTo(p) > 0) {
                        res = p;
                    }
                    System.out.println(deque.getFirst() + " " + deque.getLast());
                    System.out.println(s.substring(deque.getFirst(), deque.getLast() + 1));
                }
            }
        }
        return res;
    }
}
